using System;
using System.Data;
using Jandaya.Framework;
using Jandaya.Framework.DAO;
using FirebirdSql.Data.FirebirdClient;
	
namespace Jandaya.Framework.Core.Acesso
{
	public class DAOUsuario:DAO<Usuario>
	{
		private const string ST_USR_NOT_FOUND = "O usuário '{0}' não pôde ser encontrado!\n";
		private const string ST_PWD_NOT_VALID = "A senha informada para o usuário '{0}' é inválida.\n";
		private const string ST_USR_LOGIN_OK  = "O usuário '{0}' realizou com sucesso o login no sistema!";
		
		public DAOUsuario(Usuario pBussinessObject, FbConnection pConnection){
			BO = pBussinessObject;
			Connection = pConnection;
		}		
				
		public bool Antenciar(string pUsuario, string pSenha, out string pResult){
			pResult = string.Empty;
			
			FbCommand cmd = GetCommand("\"Login\"");			
			cmd.CommandType = CommandType.StoredProcedure;
			cmd.Parameters.Add(new FbParameter("LOGIN", FbDbType.VarChar, 20));
			cmd.Parameters.Add(new FbParameter("SENHA", FbDbType.VarChar, 80));
			cmd.Parameters.Add(new FbParameter("IDUSUARIO", FbDbType.BigInt));
			cmd.Parameters.Add(new FbParameter("ERROR", FbDbType.Integer));
			cmd.Parameters.Add(new FbParameter("MSG", FbDbType.VarChar, 120));
						
			cmd.Parameters["LOGIN"].Value = pUsuario;			    
			cmd.Parameters["SENHA"].Value = pSenha;
			cmd.Parameters["IDUSUARIO"].Direction = ParameterDirection.Output;
			cmd.Parameters["ERROR"].Direction = ParameterDirection.Output;
			cmd.Parameters["MSG"].Direction = ParameterDirection.Output;
			
			try{
				cmd.ExecuteNonQuery();
				pResult = cmd.Parameters["MSG"].Value.ToString();
				if ((Int32)cmd.Parameters["ERROR"].Value == 0){
					this.Load((Int64)cmd.Parameters["IDUSUARIO"].Value);
					return true;
				}
			}
			catch (FbException ex){
				pResult = ex.Message;
				return false;				
			}
			return false;
			
			/*
			FbDataReader rdr = GetDataReader(string.Format("select ID, senha from \"Acesso.Usuario\" where login='{0}'", pUsuario));
			bool result = (rdr.Read());
			if (!result){
				pResult = string.Format(ST_USR_NOT_FOUND, pUsuario);
				return false;
			}
			else{
				id = rdr.GetInt64(0);
				senha = rdr.GetString(1);
				if (senha != pSenha){
					pResult = string.Format(ST_PWD_NOT_VALID, pUsuario);
					return false;
				}
			}			
			if (Load(id)){
				pResult = string.Format(ST_USR_LOGIN_OK, pUsuario);
				return true;
			}
			return false;
			*/
		}
		
		public override bool Bind(DataRow pRow){	
			BO.ID.Value = Int64.Parse(pRow["id"].ToString());
			BO.Nome  = pRow["nome"].ToString();
			BO.Login = pRow["login"].ToString();
			BO.EMail = pRow["email"].ToString();
			BO.Ativo = (pRow["ativo"].ToString() == "S");			
			return true;
		}
			
		public override bool Load(Int64 ID){
			bool result = false;
			DataTable tb = GetDataTable(string.Format("select * from \"Acesso.Usuario\" where ID={0}", ID));
			if (tb.Rows.Count == 0)
				result =  false;
			else{
				result = Bind(tb.Rows[0]);
				if (result)
					BO.SetState(ObjectState.Clean);
			}
			return result;
		}
		
	}
}

